package com.kws.merchant.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kws.common.config.constant.MinioCommonBucket;
import com.kws.merchant.domain.DeviceImg;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.ServeImg;
import com.kws.merchant.mapper.DeviceImgMapper;
import com.kws.merchant.service.DeviceImgService;
import com.kws.merchant.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * (DeviceImg)表服务实现类
 *
 * @author yxy
 * @since 2024-10-22 11:03:39
 */
@Service("deviceImgService")
public class DeviceImgServiceImpl extends ServiceImpl<DeviceImgMapper, DeviceImg> implements DeviceImgService {

    @Resource
    private DeviceImgMapper deviceImgMapper;
    @Value("${minio.ENDPOINT}")
    private String minioUrl;
    /**
     * 根据设备id查询多张图片
     * @param id
     * @return
     */
    @Override
    public List<DeviceImg> listByDeviceId(Long id) {
        List<DeviceImg> list = this.list(Wrappers.<DeviceImg>lambdaQuery().eq(DeviceImg::getDeviceId, id));
        list.forEach(x->{
            x.setDeviceImg(FileUtil.fixFileName()+x.getDeviceImg());
        });
        return list;
    }

    /**
     * 添加设备图片
     * @param deviceId
     * @param imgArr
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveImgs(Long deviceId, List<String> imgArr) {
        //先删除之前图片
        this.remove(Wrappers.<DeviceImg>lambdaQuery().eq(DeviceImg::getDeviceId,deviceId));
        //再添加图片
        ArrayList<DeviceImg> serveImgs = new ArrayList<>();
        imgArr.forEach(x->{
            DeviceImg serveImg = new DeviceImg();
            serveImg.setDeviceId(deviceId);
            serveImg.setDeviceImg(FileUtil.subFileName(x));
            serveImgs.add(serveImg);
        });
        this.saveBatch(serveImgs);
    }
}
